package model.expression.command;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import model.expression.AbstractExpression;
import model.expression.constant.ConstantExpression;
import model.expression.operator.LeftBracExpression;
import model.expression.operator.RightBracExpression;
import model.parser.ParserException;

import state.TabState;

public class Tell extends AbstractCommandExpression {

	public Tell(TabState ts) {
		super(ts);
	}
	
	@Override
	protected int getExpNumChildren() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int evaluate() {
		//change the tabstate and notify observor
		//get the turtle names
		List<Integer> turtleName = new ArrayList<Integer>();
		for (AbstractExpression e : this.children){
			if (e instanceof ConstantExpression)
				turtleName.add(e.evaluate());
		}
		
		//clear the active turtles
		tabSt.clearActiveTurtles();
		
		//add turtle if new, add to the active list if already exist
		for (Integer i : turtleName){
			tabSt.addTurtle(i);
			tabSt.notifyObservers(i);
		}
		return 0;
	}
	
	public  void addChildrenFrom(Stack<AbstractExpression> out){
		if(out.empty()){
			throw ParserException.BAD_ARGUMENTS;
		}
		while(!out.isEmpty()){
			AbstractExpression node = out.pop();
			if (node instanceof LeftBracExpression)
				continue;
			if (node instanceof RightBracExpression)
				break;
			if (!(node instanceof ConstantExpression))
				throw ParserException.BAD_ARGUMENTS;
			this.children.add(node);	
		}
	}

}
